
#define ULTRASONIC_SENSOR_PIN_TRIG    A0
#define ULTRASONIC_SENSOR_PIN_ECHO    A1

#define ARC_MICROSECONDS_TO_CENTIMETERS(ms) ((ms) / 29 / 2)

unsigned char distances[10][3];

unsigned long getDistance(const int trigPin, const int echoPin)
{
  unsigned long distance, pulseDuration;

  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pulseDuration = pulseInLong(echoPin, HIGH, 3 * 2 * 1000);

  Serial.print("pulseDuration = ");
  Serial.print(pulseDuration);

  return pulseDuration;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(ULTRASONIC_SENSOR_PIN_TRIG, OUTPUT);
  pinMode(ULTRASONIC_SENSOR_PIN_ECHO, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long ret = getDistance(ULTRASONIC_SENSOR_PIN_TRIG, ULTRASONIC_SENSOR_PIN_ECHO);
  Serial.print(", dist = ");
  Serial.println(ARC_MICROSECONDS_TO_CENTIMETERS(ret));
  Serial.println(ARC_MICROSECONDS_TO_CENTIMETERS(1000));
  delay(150);
}
